package com.service.device;

import com.common.utils.DateUtils;
import com.dao.secondary.device.DevicePcbExFactoryBatchInfoMapper;
import com.github.pagehelper.PageHelper;
import com.model.domain.device.GetDevicePcbBaseInfoDO;
import com.model.domain.device.GetDevicePcbExFactoryBatchInfoDO;
import com.model.pojo.device.DevicePcbBaseInfo;
import com.model.pojo.device.DevicePcbExFactoryBatchInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @Author: zql
 * @Description: OBD设备出厂批次管理服务
 * @Data: 2023-07-09
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DevicePcbExFactoryBatchInfoService {
    private final DevicePcbExFactoryBatchInfoMapper devicePcbExFactoryBatchInfoMapper;
    private final DevicePcbBaseInfoService devicePcbBaseInfoService;

    /**
     * 分页数据
     *
     * @param getDevicePcbExFactoryBatchInfoDO
     * @return List<DevicePcbExFactoryBatchInfo>
     * @author tm
     * @date 2023-07-10 22:36
     */
    public List<DevicePcbExFactoryBatchInfo> getPageList(GetDevicePcbExFactoryBatchInfoDO getDevicePcbExFactoryBatchInfoDO) {
        PageHelper.clearPage();
        PageHelper.startPage(getDevicePcbExFactoryBatchInfoDO.getPage(), getDevicePcbExFactoryBatchInfoDO.getPageSize(), getDevicePcbExFactoryBatchInfoDO.getSortRow() + getDevicePcbExFactoryBatchInfoDO.getSortOrder());
        return devicePcbExFactoryBatchInfoMapper.selectList(getDevicePcbExFactoryBatchInfoDO);
    }

    public List<DevicePcbExFactoryBatchInfo> getAllList(GetDevicePcbExFactoryBatchInfoDO getDevicePcbExFactoryBatchInfoDO) {
        PageHelper.clearPage();
        PageHelper.orderBy(getDevicePcbExFactoryBatchInfoDO.getSortRow() + getDevicePcbExFactoryBatchInfoDO.getSortOrder());
        return devicePcbExFactoryBatchInfoMapper.selectList(getDevicePcbExFactoryBatchInfoDO);
    }

    public int save(DevicePcbExFactoryBatchInfo batchInfo) {
        batchInfo.setCreateTime(new Date());
        return devicePcbExFactoryBatchInfoMapper.insertSelective(batchInfo);
    }

    public DevicePcbExFactoryBatchInfo getById(long batchId) {
        return devicePcbExFactoryBatchInfoMapper.selectByPrimaryKey(batchId);
    }

    public int update(DevicePcbExFactoryBatchInfo batchInfo) {
        batchInfo.setLastModifiedTime(new Date());
        return devicePcbExFactoryBatchInfoMapper.updateByPrimaryKeySelective(batchInfo);
    }

    @Transactional
    public int updateExFactory(DevicePcbExFactoryBatchInfo batchInfo) {
        int num = 0;
        boolean flag = true;
        flag = false;
        Date serviceStartTime = new Date();
        int serviceYear = batchInfo.getServiceYear();
        Date serviceEndTime = DateUtils.addDate(serviceStartTime,serviceYear,0,0,0,0,0,0);
        GetDevicePcbBaseInfoDO getDevicePcbBaseInfoDO = new GetDevicePcbBaseInfoDO();
        getDevicePcbBaseInfoDO.setBatchId(batchInfo.getBatchId());
        getDevicePcbBaseInfoDO.setSortRow("id");
        List<DevicePcbBaseInfo> list = new ArrayList<>();
        List<DevicePcbBaseInfo> baseList = devicePcbBaseInfoService.getAllList(getDevicePcbBaseInfoDO);
        if (baseList != null && baseList.size() > 0) {
            for (DevicePcbBaseInfo info : baseList) {
                info.setServiceStartTime(serviceStartTime);
                info.setServiceEndTime(serviceEndTime);
                info.setLastModifiedTime(new Date());
                list.add(info);
            }
            devicePcbBaseInfoService.updateList(list);
            flag = true;
        }
        if (flag) {
            batchInfo.setLastModifiedTime(new Date());
            num = devicePcbExFactoryBatchInfoMapper.updateByPrimaryKeySelective(batchInfo);
        }
        return num;
    }
}
